package com.annhoa.interfaces.facade;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.annhoa.domain.entity.SysUser;
import com.annhoa.domain.req.SysUserReq;
import com.annhoa.domain.service.SysUserService;
import com.annhoa.infrastructure.common.AjaxResult;
import com.annhoa.infrastructure.common.BasePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import com.annhoa.infrastructure.common.PageData;

/**
 * 
 * @author admin
 * @RestController:@ResponseBody和@Controller的合集
 * 
 */
@RestController
@RequestMapping(value = "/users")
public class SysUserController extends BaseController{
	
	@Autowired
	private SysUserService userService;

	@PostMapping(value = "/add")
	public AjaxResult addUser(@RequestBody SysUserReq req) {
		userService.saveUser(req);
		return AjaxResult.success();
	}

	@DeleteMapping(value = "/delete/{id}")
	public void delete(@PathVariable int id) {
		userService.delete(id);
	}

	//添加登录拦截
	@PostMapping(value = "/findByParams")
	public List<PageData> findByParams(@RequestBody PageData pd) throws Exception {
		if (pd==null) {
			pd = new PageData();
		}
	    return userService.findForList(pd);
	}
	@GetMapping(value = "/findUser")
	public List<SysUser> findUser() throws Exception {
		return userService.findForList(new SysUser());
	}
	//分页
	@PostMapping("/pageList")
	public Page<SysUser> pageList(int page, int size) {
		PageRequest request = PageRequest.of(page-1, size);
		return userService.pageAll(request);
	}

	@PostMapping(value = "/basePageList")
	public Page<Map> basePageList(BasePage page) throws Exception {
		if (page==null) {
			page = new BasePage();
		}
		return userService.pageList(page);
	}

	@GetMapping(value = "/{id}")
	public SysUser getUser(@PathVariable int id) {
		return userService.findById(id);
	}
	
}